package com.hope.onlinestudy.fragment.user

import android.view.View
import com.bumptech.glide.Glide
import com.hope.onlinestudy.BuildConfig
import com.hope.onlinestudy.R
import com.hope.onlinestudy.activity.chanagepass.ChanagePassActivity
import com.hope.onlinestudy.activity.info.InfoActivity
import com.hope.onlinestudy.activity.integral.IntegralActivity
import com.hope.onlinestudy.activity.login.LoginActivity
import com.hope.onlinestudy.activity.message.MessageActivity
import com.hope.onlinestudy.activity.webview.WebViewActivity
import com.hope.onlinestudy.img.GlideCircleTransform
import com.hope.onlinestudy.model.UserInfoModel
import com.hope.onlinestudy.mvp.MVPBaseFragment
import com.hope.onlinestudy.utils.ApiUtils
import com.hope.onlinestudy.view.BadgeView
import kotlinx.android.synthetic.main.fragment_user.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.support.v4.startActivity

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */
class UserFragment : MVPBaseFragment<UserContract.View, UserPresenter>(), View.OnClickListener,
    UserContract.View {
    override fun initView(): Int {
        return R.layout.fragment_user
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rlChangePass -> startActivity<ChanagePassActivity>()
            R.id.rlInfo -> startActivity<InfoActivity>("data" to mPresenter?.userInfo)
            R.id.rlJf -> startActivity<IntegralActivity>("data" to mPresenter?.userInfo)
            R.id.rlMessage -> startActivity<MessageActivity>()
            R.id.rlOrder -> startActivity<WebViewActivity>("url" to ApiUtils.toMyOrder)
//            R.id.rlOrder -> {
//                activity?.supportFragmentManager?.beginTransaction()
//                    ?.replace(R.id.flFrame, WebFragment.instance(ApiUtils.toMyOrder))?.commit()
//            }
            R.id.rlZy -> startActivity<WebViewActivity>("url" to ApiUtils.myhomework)
            R.id.btnExit -> mPresenter?.exitApp()
            R.id.rlLesson -> startActivity<WebViewActivity>("url" to ApiUtils.toMyCourse)
            R.id.rlNote -> startActivity<WebViewActivity>("url" to ApiUtils.toMyNote)
            R.id.rlPlan -> startActivity<WebViewActivity>("url" to ApiUtils.lessonplan)
            R.id.rlCn -> startActivity<WebViewActivity>("url" to ApiUtils.testpage)
        }
    }

    override fun bindData() {
        rlChangePass.setOnClickListener(this)
        rlInfo.setOnClickListener(this)
        rlJf.setOnClickListener(this)
        rlMessage.setOnClickListener(this)
        rlOrder.setOnClickListener(this)
        rlZy.setOnClickListener(this)
        btnExit.setOnClickListener(this)
        rlLesson.setOnClickListener(this)
        rlNote.setOnClickListener(this)
        rlPlan.setOnClickListener(this)
        rlCn.setOnClickListener(this)

        mPresenter?.getMsgAlert()
        mPresenter?.getUserInfo()
    }

    override fun lazyLoad() {

    }

    override fun setInfo(info: UserInfoModel?) {
        tvName.text = info?.truename
        Glide.with(this)
            .load("${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_file_context}${info?.userImg}")
            .asBitmap()
            .placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher)
            .transform(GlideCircleTransform(activity!!)).into(ivUserIcon)
    }

    override fun setAlertLength(len: Int?) {
        if (len != 0) {
            tvMsgSize.visibility = View.VISIBLE
            val badge = BadgeView(activity)
            badge.setTargetView(tvMsgSize)
            badge.badgeCount = len
        }
    }

    override fun exitLogin() {
        baseActivity.myApplicaton?.exitApp()
        baseActivity.startActivity<LoginActivity>()
        baseActivity.finish()
    }
}